package com.example.manage_dzensaas.controller;

import com.example.manage_dzensaas.core.utils.StringUtils;
import com.example.manage_dzensaas.pojo.bo.AllProductsBo;
import com.example.manage_dzensaas.pojo.bo.ProductInfoBo;
import com.example.manage_dzensaas.pojo.entity.db1.ProductInfo;
import com.example.manage_dzensaas.pojo.result.ApiResult;
import com.example.manage_dzensaas.service.ProductInfoService;
import io.swagger.annotations.*;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.UploadedFile;
import org.noear.solon.validation.annotation.NoRepeatSubmit;

import java.util.List;

@Api("商品管理")
@Controller
@Mapping("/products")
public class ProductInfoController {

    @Inject
    ProductInfoService productInfoService;

    @ApiOperation(value = "新增商品", notes = "添加新的商品信息")
    @NoRepeatSubmit
    @Post
    @Mapping("/add")
    public ApiResult addProductInfo(@Body ProductInfoBo productInfoBo) {
        return productInfoService.addProductInfo(productInfoBo);
    }

    @ApiOperation(value = "通过Excel文件更新商品信息", notes = "上传Excel文件批量更新商品信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "Excel文件", required = true, dataType = "file", paramType = "form"),
        @ApiImplicitParam(name = "addedBy", value = "操作人", required = true, dataType = "string", paramType = "query")
    })
    @NoRepeatSubmit
    @Post
    @Mapping("/update")
    public ApiResult updateFromExcel(@Param(value = "file", required = true) UploadedFile file,
                                     @Param(value = "addedBy", required = true) String addedBy) {
        if (!"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(file.getContentType())) {
            return ApiResult.error("只支持xlsx文件格式");
        }
        if (StringUtils.isBlank(addedBy)) {
            return ApiResult.error("操作人不能为空");
        }
        productInfoService.importFromExcel(file, addedBy);
        return ApiResult.success("xlsx文件上传成功");
    }

    @ApiOperation(value = "通过CSV文件更新商品信息", notes = "上传CSV文件批量更新商品信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "CSV文件", required = true, dataType = "file", paramType = "form"),
        @ApiImplicitParam(name = "addedBy", value = "操作人", required = true, dataType = "string", paramType = "query")
    })
    @NoRepeatSubmit
    @Post
    @Mapping("/upload-csv")
    public ApiResult uploadCsv(@Param(value = "file", required = true) UploadedFile file,
                               @Param(value = "addedBy", required = true) String addedBy) {
        // 校验文件类型
        if (!"text/csv".equals(file.getContentType())) {
            return ApiResult.error("只支持csv文件格式");
        }
        if (StringUtils.isBlank(addedBy)) {
            return ApiResult.error("操作人不能为空");
        }
        productInfoService.importFromCSV(file, addedBy);
        return ApiResult.success("CSV文件上传并处理成功");
    }

    @ApiOperation(value = "查询所有商品", notes = "查询最新添加的10条商品，主要用来查询测试数据")
    @Get
    @Mapping("/all")
    public ApiResult<List<ProductInfo>> getAllProducts(@ApiParam(value = "查询条件", required = true)
                                                       @Body AllProductsBo allProductsBo) {
        List<ProductInfo> products = productInfoService.findAll(allProductsBo);
        return ApiResult.success(products);
    }

    @ApiOperation(value = "查询所有商品V2", notes = "查询最新添加的10条商品，主要用来查询测试数据V2")
    @Get
    @Mapping("/allV2")
    public ApiResult<List<ProductInfo>> getAllProductsV2(@ApiParam(value = "查询条件", required = true)
                                                       @Body AllProductsBo allProductsBo) {
        List<ProductInfo> products = productInfoService.findAll(allProductsBo);
        return ApiResult.success(products);
    }
}
